// TireInputView.java
package com.tzkyzj.tiremanagersystem.ui;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.tzkyzj.tiremanagersystem.R;
import com.tzkyzj.tiremanagersystem.eneities.TireInfo;

public class TireInputView extends LinearLayout {

    private TextView tvPosition;
    private EditText etTireNumber, etTireBrand, etTireModel, etTirePrice, etInstallCost;
    private Button btnAddPhotos;
    private TextView tvPhotoCount;

    private int axleNumber;
    private String tirePosition;
    private int tireIndex;

    public TireInputView(Context context) {
        super(context);
        init(context);
    }

    public TireInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TireInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public TireInputView(Context context, int axleNumber, String tirePosition) {
        super(context);
        this.axleNumber = axleNumber;
        this.tirePosition = tirePosition;
        this.tireIndex = calculateTireIndex(axleNumber, tirePosition);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_tire_input, this, true);

        tvPosition = findViewById(R.id.tv_position);
        etTireNumber = findViewById(R.id.et_tire_number);
        etTireBrand = findViewById(R.id.et_tire_brand);
        etTireModel = findViewById(R.id.et_tire_model);
        etTirePrice = findViewById(R.id.et_tire_price);
        etInstallCost = findViewById(R.id.et_install_cost);
        btnAddPhotos = findViewById(R.id.btn_add_photos);
        tvPhotoCount = findViewById(R.id.tv_photo_count);

        setupViews();
    }

    private void setupViews() {
        // 设置位置显示
        String positionText = axleNumber + "轴" + tirePosition;
        tvPosition.setText(positionText);

        // 设置照片按钮点击事件
        btnAddPhotos.setOnClickListener(v -> {
            if (getContext() instanceof BatchAddTireActivity) {
                ((BatchAddTireActivity) getContext()).selectPhotos(tireIndex);
            }
        });

        // 设置默认值
        etTirePrice.setText("0");
        etInstallCost.setText("0");
    }

    private int calculateTireIndex(int axle, String position) {
        int index = 0;
        String[][] positions = {
                {"左外", "右外"},
                {"左外", "左内", "右内", "右外"},
                {"左外", "左内", "右内", "右外"},
                {"左外", "左内", "右内", "右外"},
                {"左外", "左内", "右内", "右外"},
                {"左外", "左内", "右内", "右外"}
        };

        for (int i = 0; i < axle - 1; i++) {
            index += positions[i].length;
        }

        for (int i = 0; i < positions[axle - 1].length; i++) {
            if (positions[axle - 1][i].equals(position)) {
                return index + i;
            }
        }

        return index;
    }

    public TireInfo getTireInfo() {
        String tireNumber = etTireNumber.getText().toString().trim();
        if (TextUtils.isEmpty(tireNumber)) {
            return null;
        }

        TireInfo tireInfo = new TireInfo();
        tireInfo.setTireNumber(tireNumber);
        tireInfo.setTireBrand(etTireBrand.getText().toString().trim());
        tireInfo.setTireModel(etTireModel.getText().toString().trim());
        tireInfo.setAxleNumber(axleNumber);
        tireInfo.setTirePosition(tirePosition);

        try {
            tireInfo.setTirePrice(Double.parseDouble(etTirePrice.getText().toString().trim()));
        } catch (NumberFormatException e) {
            tireInfo.setTirePrice(0.0);
        }

        try {
            tireInfo.setInstallCost(Double.parseDouble(etInstallCost.getText().toString().trim()));
        } catch (NumberFormatException e) {
            tireInfo.setInstallCost(0.0);
        }

        return tireInfo;
    }

    public String getTireNumber() {
        return etTireNumber.getText().toString().trim();
    }

    public void setTireIndex(int index) {
        this.tireIndex = index;
    }

    public int getTireIndex() {
        return tireIndex;
    }

    public void updatePhotoCount(int count) {
        tvPhotoCount.setText("已选择: " + count + "/3");
    }
}